package hongcheng.code_generator;

import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.stream.Collectors;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.extra.template.Template;
import cn.hutool.extra.template.TemplateConfig;
import cn.hutool.extra.template.TemplateConfig.ResourceMode;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.TemplateExceptionHandler;
import hongcheng.code_generator.config.DefaultXMLConfigReader;
import hongcheng.code_generator.database.DefaultMySqlDBMetadataReader;
import hongcheng.code_generator.entity.db.TableMetadata;
import cn.hutool.extra.template.TemplateEngine;
import cn.hutool.extra.template.TemplateUtil;

public class T {

	
	@Test
	public void aaaa44444aaa() throws IOException {
		File f = new File("main/resources/template/entity");
		System.err.println(f);
		System.err.println(f.getCanonicalPath());
		System.err.println(f.getName());
	}
	
	
	
	@Test
	public void aaaaaaa() {
		Class<? extends ClassLoader> class1 = Thread.currentThread().getContextClassLoader().getClass();
		System.err.println(Configuration.class.getResource(""));
		System.err.println(Configuration.class.getResource("/"));
	}
	
	
	@Test
	public void tteeet() throws Exception {
		String xmlConfigFilePath = "D:\\workspace\\java_web\\code-generator\\NewFile.xml";
		DefaultXMLConfigReader xmlConfigReader = new DefaultXMLConfigReader(xmlConfigFilePath);
		DefaultMySqlDBMetadataReader dbMetadataReader = new DefaultMySqlDBMetadataReader(xmlConfigReader.getCodeGeneratorConfig());
		TableMetadata dbMetadata = dbMetadataReader.getDBMetadata();
		
		Writer out = new OutputStreamWriter(System.out);
		
		Set<String> javaClassSet = dbMetadata.getTableColumnList().parallelStream().map((tableColumnMetadata)->{
			return tableColumnMetadata.getJavaClassName();
		}).collect(Collectors.toSet());
		LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>();
		map.put("package", "hongcheng.code_generator");
		map.put("entityClassName", "CityDO");
		map.put("voClassName", "CityQueryVO");
		map.put("daoClassName", "CityMapper");
		map.put("serviceClassName", "CityService");
		map.put("controllerClassName", "CityController");
		map.put("author", "hongcheng");
		map.put("date", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
		map.put("table", dbMetadata);
		map.put("javaClassSet", javaClassSet);
		
		Configuration controllerConfiguration = new Configuration(Configuration.VERSION_2_3_29);
		controllerConfiguration.setDefaultEncoding("UTF-8");
		controllerConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
		controllerConfiguration.setLocalizedLookup(false);
		String templetFilePath = "D:\\workspace\\java_web\\code-generator\\target\\test-classes";
		controllerConfiguration.setTemplateLoader(new FileTemplateLoader(new File(templetFilePath)));
		freemarker.template.Template template2 = controllerConfiguration.getTemplate("entity.ftl");
		
		//假设我们引入的是Beetl引擎，则：
		template2.process(map, out);
	}
	
	
	
	
	@Test
	public void ttt() {
		String filePath = "C:\\Users\\HongCheng\\Desktop\\1.xml";
		Document document = null;
		try {
			//读取指定的xml文件之后返回一个Document对象，这个对象代表了整个XML文档，用于各种Dom运算。执照XML文件头所定义的编码来转换。
			SAXReader saxReader = new SAXReader();
			//读取XML文件,获得document对象
			document = saxReader.read(new File(filePath)); 
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		System.err.println(document.getUniquePath());
//		Element rootElement = document.getRootElement();
//		List selectNodes = rootElement.selectNodes("//book");
//		for(Object obj:selectNodes) {
//			Element e = (Element)obj;
//			System.err.println(e.asXML());
//		}
//		Element newElement = element.addElement("测试节点");
//		
//		
//		XMLWriter writer = null;
//		try {
//			OutputFormat of = OutputFormat.createPrettyPrint();
//			of.setEncoding("UTF-8");
//			of.setIndentSize(4);
//			writer = new XMLWriter(System.out,of);
//			writer.write(document);
//			writer.flush();
//			writer.close();
//			System.err.println("333");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
	}
}
